Skip to content

procstats: support cgroup v2 - #238

Merged
sathvik09 merged 3 commits into
mainfrom
fix/procstats-cgroup-v2
Oct 30, 2026
Merged

sathvik09 merged 3 commits into
mainfrom
fix/procstats-cgroup-v2

Conversation

@sathvik09

@sathvik09 sathvik09 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Problem

procstats only read cgroup v1 files (cpu.cfs_quota_us, cpu.cfs_period_us, cpu.shares, memory.limit_in_bytes) and dropped the 0::/path line in /proc/<pid>/cgroup. On cgroup v2 pods the reads failed silently, so CPU percent fell back to 1/NumCPU and the reported memory limit (Available) to 0.

Change

  • Detect v2 once via /sys/fs/cgroup/cgroup.controllers; v1 and hybrid systems keep the existing paths.
  • CPU quota/period from cpu.max (max reported as -1µs, matching v1; period defaults to 100ms if absent; malformed input is rejected).
  • CPU shares derived from cpu.weight, mapping the v2 default (100) to the v1 default (1024) so the existing shares/1024 ratio still applies.
  • Memory limit from memory.max (max treated as unlimited).
  • Parse the unified 0:: entry and add ProcCGroup.Unified(); proc_linux.go falls back to it when there is no v1 cpu,cpuacct entry.
  • Fall back to sysinfo (total RAM) when no memory cgroup is found, instead of reporting 0. This also fixes the same case on v1.
  • Read the small cgroup files with a single open/read/close on Linux (falls back to os.ReadFile on error or large files).

Testing

  • Unit tests for unified-entry parsing, cpu.max parsing (including malformed input) and weight→shares (procstats/linux/cgroup_v2_test.go).
  • go test ./procstats/linux passes on linux/arm64 in a cgroup v2 container (TestReadProcSched skipped: Docker Desktop's kernel has no /proc/<pid>/sched, unrelated) and on macOS.
  • Verified in a real cgroup v2 container (docker run --cpus=1.5 --memory=256m, stat -fc %T /sys/fs/cgroup = cgroup2fs):
main this PR
CPU period 0 (no such file) 100ms
CPU quota 0 (no such file) 150ms
CPU shares 0 (no such file) 1024
Memory limit 0 268435456 (256MiB)
  • v1: checked against fake v1 cgroup files; CPU and memory results match main. ParseProcCGroup now also returns the 0:: entry on hybrid hosts.

Benchmarks

New benchmarks: BenchmarkParseProcCGroup (v1/v2), BenchmarkParseCPUMax, and live-file BenchmarkReadCPU{Period,Quota,Shares} / BenchmarkReadMemoryLimit.

Run on linux/arm64 in a cgroup v2 Docker container, laptop on AC power with no video/music running; 10 runs per side, main and this PR interleaved each round, compared with benchstat. Benchmarks use b.Loop() so results that are discarded can't be optimized away:

Benchmark main this PR
ReadCPUPeriod/Quota/Shares ~745ns, 176–240 B, 5 allocs — returns 0 ~1.11µs, 112–132 B, 5 allocs
ReadMemoryLimit 2.6µs, 10 allocs — returns 0 6.3µs, 20 allocs
ParseProcCGroup/v1 683ns 676ns (~, p=1.00)
ParseProcCGroup/v2 21ns — drops the line 43ns, 1 alloc
ParseCPUMax — 27ns, 0 allocs

main does not support v2, so its read numbers are the cost of failing to find the v1 files, not a working baseline. The added cost is ~0.4µs per CPU read and ~4µs per memory read, once per collection. v1 parsing is unchanged.

Review pass

Asked Claude to review the first version for performance and correctness. Applied: caching the v2 check (was an os.Stat per read), the lighter small-file read, the memory 0→sysinfo fallback, the cpu.weight mapping, and stricter cpu.max parsing.

Not addressed here: cpu.max is still read twice per collection (avoiding it needs a new exported function), and reading another pid's cgroup or limits set on a parent cgroup are unchanged from v1 behaviour.

🤖 Generated with Claude Code

sathvik09 and others added 3 commits October 30, 2026 09:56
Read CPU quota/period from cpu.max, shares from cpu.weight, and the memory
limit from memory.max when /sys/fs/cgroup is the unified hierarchy. Parse the
"0::/path" entry in /proc/<pid>/cgroup, which was previously dropped, so v2
pods no longer fall back to 1/NumCPU and total system memory.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Add benchmarks for parsing /proc/<pid>/cgroup and cpu.max and for reading the
live cgroup CPU and memory files.

From review:
- Check for cgroup v2 once instead of an os.Stat on every read.
- Read small cgroup files with a single open/read/close on Linux.
- Fall back to sysinfo when no memory cgroup is found, instead of reporting a
  limit of 0.
- Map cpu.weight so the v2 default (100) matches the v1 default shares (1024).
- Reject malformed cpu.max and default the period to 100ms when absent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
b.Loop keeps the compiler from eliminating calls whose results are
discarded, which matters for the pure parseCPUMax benchmark.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sathvik09
sathvik09 merged commit a3223fd into main Oct 30, 2026
14 checks passed
@sathvik09
sathvik09 deleted the fix/procstats-cgroup-v2 branch October 30, 2026 05:51
@sathvik09 sathvik09 mentioned this pull request Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants